import LoanFeeReportService from "../report-private-service/loan-fee-report.service";
import { getLocalStorageWithAutoParse, messages } from "share/common";
import { COMMON_REPORT_CONFIG, LOAN_BALANCE_COLUMNS, LOAN_DETAIL_COLUMNS } from "../constant";
import ReportOverview from "components/template/graphic-report/report-overview";
import React from "react";
import Charts from "components/template/graphic-report/report-echarts";
import VScrollEchartsV2 from "components/template/graphic-report/vscroll-echarts";
import ReportTable from "components/template/graphic-report/report-table";
import { commonUtilsFactory, toggleShowLabel } from "../echarts-data-transfer/utils";
import GraphicReportService from "../graphic-report.service";

const reportKeyPrefix = 'hly.reportLabel.10012.';
const loanTendencyLabelKey = `${reportKeyPrefix}loanTendencyReport`;
const loanTypeLabelKey = `${reportKeyPrefix}loanTypeReport`;
const loanAnalysisLabelKey = `${reportKeyPrefix}loanAnalysisReport`;

function exportUtilsFactory({ exportType, config, context }) {
  const reportCode = 'CAAR';
  let ref = config.ref || {
    state: {}
  };
  let { list = [] } = ref.state;
  let exportDisable = !list.length;
  const { reportId } = context.state;
  let transferredParams = GraphicReportService.getCurrentTransferredParams({ context });
  let exportParams = { reportSearchDTO: { id: reportId, ...transferredParams } };
  return commonUtilsFactory({
    allowSetting: false,
    allowExport: true,
    reportCode,
    exportDisable,
    exportParams,
    exportType
  });
}

export default (context) => {
  let loanTendencyShowLabel = getLocalStorageWithAutoParse(loanTendencyLabelKey);
  if (loanTendencyShowLabel === undefined) {
    loanTendencyShowLabel = true;
  }

  let expenseTypeShowLabel = getLocalStorageWithAutoParse(loanTypeLabelKey);
  if (expenseTypeShowLabel === undefined) {
    expenseTypeShowLabel = true;
  }

  let loanAnalysisShowLabel = getLocalStorageWithAutoParse(loanAnalysisLabelKey);
  if (loanAnalysisShowLabel === undefined) {
    loanAnalysisShowLabel = true;
  }


  return {
    loanOverviewReport: {
      isLoading: false,
      fetchMethod: LoanFeeReportService.getTotalAnalysisReport,
      options: [{
        title: messages('finance-7.key203')/*累计借款金额*/,
        detail: 0
      }, {
        title: messages('finance-7.key204')/*实时借款金额*/,
        detail: 0
      }, {
        title: messages('finance-7.key205')/*总借款人数（含供应商）*/,
        detail: 0
      }],
      render: ({ config }) => {
        return (
          <div className="report-row">
            <ReportOverview
              data={config.options}
            />
          </div>
        )
      }
    },
    loanTendencyReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key206')/*借款总体趋势报表*/,
      fetchMethod: LoanFeeReportService.getLoanTendencyReport,
      showLabel: loanTendencyShowLabel,
      activeConfig: {
        key: 'filterYearMonths',
        array: true
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const utils = commonUtilsFactory({ context, reportType, config, reportKeyPrefix });
        options = toggleShowLabel({ config, options });
        return (
          <Charts
            allowActivated
            title={title}
            cols={24}
            utils={utils}
            dataLength={dataLength}
            options={options}
            reportKey='loanTendencyReport'
            reportType='tendency'
            isFetchingData={isFetchingData}
            loading={isLoading}
            compareKey='monthName'
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    loanTypeReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key207')/*借款类型分析*/,
      fetchMethod: LoanFeeReportService.getLoanTypeReport,
      showLabel: expenseTypeShowLabel,
      activeConfig: {
        key: 'filterLoanTypeName',
        array: false
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const utils = commonUtilsFactory({ context, reportType, config, reportKeyPrefix });
        options = toggleShowLabel({ config, options });
        return (
          <VScrollEchartsV2
            allowActivated
            title={title}
            dataLength={dataLength}
            options={options}
            cols={12}
            utils={utils}
            reportKey='loanTypeReport'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    loanAnalysisReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key208')/*借款对象分析*/,
      fetchMethod: LoanFeeReportService.getLoanAnalysisReport,
      showLabel: loanAnalysisShowLabel,
      activeConfig: {
        key: {
          filterPayeeType: {
            array: false
          },
          filterPayeeId: {
            array: false
          }
        },
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const utils = commonUtilsFactory({ context, reportType, config, reportKeyPrefix });
        options = toggleShowLabel({ config, options });
        return (
          <VScrollEchartsV2
            allowActivated
            title={title}
            dataLength={dataLength}
            options={options}
            cols={12}
            utils={utils}
            reportKey='loanAnalysisReport'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    loanDetailReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key209')/*借款明细*/,
      fetchMethod: LoanFeeReportService.getLoanDetailReport,
      isTable: true,
      columns: LOAN_DETAIL_COLUMNS,
      render: ({ config, context }) => {

        const exportType = 'NO_GENERAL_REPORT_LOAN_ANALYSIS_DETAIL';
        const utils = exportUtilsFactory({
          config,
          context,
          exportType
        });

        return (
          <ReportTable
            title={config.title}
            cols={24}
            utils={utils}
            columns={config.columns}
            fetchMethod={config.fetchMethod}
            onRef={context.onMountCharts}
            dataLength={1}
            reportKey='loanDetailReport'
          />
        )
      }
    },
    loanBalanceReport: {
      ...COMMON_REPORT_CONFIG,
      title: (context) => (
        <section>{messages('finance-7.key146')/*借款余额*/}
          <span className="report-wrapper-title-desc">
        {context.state.searchParams && context.state.searchParams.filterBalanceType === '2' ? messages('finance-7.key210')/*以单据支付通过日期为准统计的借款余额*/ : messages('finance-7.key211')/*以单据审核通过日期为准统计的借款余额*/}
          </span>
        </section>
      ),
      fetchMethod: LoanFeeReportService.getLoanBalanceDetail,
      isTable: true,
      columns: LOAN_BALANCE_COLUMNS,
      render: ({ config, context }) => {

        const exportType = 'NO_GENERAL_REPORT_LOAN_ANALYSIS_BALANCE';
        const utils = exportUtilsFactory({
          config,
          context,
          exportType
        });

        return (
          <ReportTable
            title={config.title(context)}
            cols={24}
            utils={utils}
            columns={config.columns}
            fetchMethod={config.fetchMethod}
            onRef={context.onMountCharts}
            dataLength={1}
            reportKey='loanBalanceReport'
          />
        )
      }
    },
  }
}
